package com.example.departmentService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.departmentService.entity.ProjectInfo;
import com.example.departmentService.entity.ProjectMember;
import com.example.departmentService.entity.dto.ProjectListDto;
import com.example.departmentService.entity.dto.QueryConditions;
import com.example.departmentService.entity.vo.ProjectListVo;
import com.example.departmentService.mapper.ProjectInfoMapper;
import com.example.departmentService.mapper.ProjectMemberMapper;
import com.example.departmentService.service.IProjectInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements IProjectInfoService {
    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private ProjectMemberMapper projectMemberMapper;

    @Override
    public List<ProjectListVo> queryList(QueryConditions queryConditions) {
        QueryWrapper<ProjectInfo> wrapper = new QueryWrapper<>();
        if (!Objects.equals(queryConditions.getProjectName(), ""))
            wrapper.like("PROJECT_NAME", queryConditions.getProjectName());
        if (queryConditions.getStartTime() != null) wrapper.ge("START_TIME", queryConditions.getStartTime());
        if (queryConditions.getEndTime() != null) wrapper.le("END_TIME", queryConditions.getEndTime());

        List<ProjectInfo> projectInfos = projectInfoMapper.selectList(wrapper);
        List<ProjectListVo> projectListVos = new ArrayList<>();
        for (ProjectInfo item : projectInfos) {
            ProjectListVo projectListVo = new ProjectListVo();
            BeanUtil.copyProperties(item, projectListVo);
            switch (projectListVo.getState()) {
                case "0":
                    projectListVo.setState("进行中");
                    break;
                case "1":
                    projectListVo.setState("已完成");
                    break;
            }
            List<ProjectMember> projectMembers = projectMemberMapper.selectList(new QueryWrapper<ProjectMember>().eq("PROJECT_ID", item.getId()));
            List<String> members = new ArrayList<>();
            for (ProjectMember projectMember : projectMembers) {
                members.add(projectMember.getProjectMember());
            }
            projectListVo.setProjectMember(members);
            projectListVos.add(projectListVo);
        }
        return projectListVos;
    }

    @Override
    public Integer insert(ProjectListDto projectListDto) {
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setProjectId(projectListDto.getProjectId());
        projectInfo.setProjectName(projectListDto.getProjectName());
        projectInfo.setProjectLeader(projectListDto.getProjectLeader());
        projectInfo.setStartTime(projectListDto.getStartTime());
        projectInfo.setEndTime(projectListDto.getEndTime());
        projectInfo.setProjectInfo(projectListDto.getProjectInfo());
        projectInfo.setState("0");
        int item = projectInfoMapper.insert(projectInfo);
        for (String s : projectListDto.getProjectMember()) {
            ProjectMember projectMember = new ProjectMember();
            projectMember.setProjectId(projectListDto.getProjectId());
            projectMember.setProjectMember(s);
            projectMemberMapper.insert(projectMember);
        }
        return item;
    }
}
